/**
 * Created by will on 2017/9/27.
 */
$(function () {
    layui.use(['upload','layer'], function(){
        var upload = layui.upload
            ,layer=layui.layer;
        //普通图片上传
        var uploadInst = upload.render({
            elem: '.roompic .uploadpic'
            ,url: '/upload/'
            ,before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    /*$('#demo1').attr('src', result); //图片链接（base64）*/
                    $('.uploadpic').parent().parent().siblings('.layui-upload-list').append('<img src="'+ result +'" alt="'+ file.name +'" class="layui-upload-img">')
                });
            }
            ,done: function(res){
                //如果上传失败
                if(res.code > 0){
                    return layer.msg('上传失败');
                }
                //上传成功
            }
            ,error: function(){
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
        });
     });
    //新增下一行
    $('#add-newrow').click(function () {
        var newinput = $('<tr>'
            +'<td>'
            +'<input type="text" placeholder="请输入">'
            +'</td>'
            +'<td>'
            +'<div class="img-contain">'
            +'<img src="../images/hotel1.png" alt="">'
                +'<i>'
                +'<img src="../images/delete.png" alt="">'
            +'</i>'
            +'</div>'
            +'<div class="img-contain"  style="margin: 0 5px">'
            +'<img src="../images/hotel2.png" alt="">'
            +'<i>'
            +'<img src="../images/delete.png" alt="">'
            +'</i>'
            +'</div>'
            +'<div class="img-contain">'
            +'<img src="../images/hotel3.png" alt="">'
            +'<i>'
            +'<img src="../images/delete.png" alt="">'
            +'</i>'
            +'</div>'
            +'</td>'
            +'<td>'
            +'<div class="layui-upload">'
            +'<span  class="uploadpic">'
        +'上传图片'
            +'</span>'
            +'</div>'
            +'<a href="javascript:;">'
            +
            '删除行'
            +'</a>'
            +'</td>'
            +'</tr>')
        $(this).prev().append(newinput)
    })
//    房型图片中删除当前行
    $(".roompic").on('click','table>tbody>tr>td>a',function () {
        $(this).parent().parent().remove();
        console.log(1);
    })
//    房型图片删除图片
    $('.roompic').on('click','table>tbody>tr>td>div>i',function(){
        $(this).parent().remove();
    })
    //删除基础设施中的某一行
    $('.formcontainer').on('click','form>div>a',function(){
        $(this).parent().remove()
    })
//基础设施中的新增下一行
    $('#add-newinput').on('click',function(){
        var newinput2 = $('<div>'
            +'<input type="text" name="title" required lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input">'
            +'<a href="javascript:;">'
            +
            '删除'
            +'</a>'
                +'<div class="layui-form-item layui-form-text">'
                +
            '<div class="layui-input-block">'
                +
            '<textarea name="desc" placeholder="请输入内容" class="layui-textarea">' +
            '</textarea>'
            +'</div>'
            +'</div>'
            +'</div>')
        $(this).siblings('form').append(newinput2)
    })
//    点击取消返回上一页
   /* $(".cancel").click(function(){
        window.location.href="./easytrip-roominfo.html"
    })
    //    点击保存返回上一页
    $(".save").click(function(){
        window.location.href="./easytrip-roominfo.html"
    })*/

})